package com.star.exam;

/**
 * @author: liminghui
 * @date: 2021/9/8 19:51
 * @version: 1.0
 * @description:
 */
public class ExamTest1 {
    public static void main(String[] args) {

        /**
         * abc  abcbc abc+bc=>2
         * abcd abcdec =>-1
         * xyz  xzyxz xz+y+xz=>3
         * axyz  xzyaxz xz+y+axz=>3
         */
        String a = "abcd";
        String b = "abcdbd";
        int subStr = subStrACount(a, b);
        System.out.println(subStr);

    }

    public static int subStrACount(String a, String b) {
        int left = 0;
        int right = 0;
        // 判断相邻两个字符满足:在A中的索引位置必须为升序.  b在A索引1,a在A索引为0
        int result = 1;
        char[] charsB = b.toCharArray();
        char[] charsA = a.toCharArray();
        for (int i = 0; i < charsB.length; i++) { // B字符串abcbc
            // 判断b中字符全部来源于a中字符,否则返回-1
            if (!a.contains(String.valueOf(charsB[i]))) {
                return -1;
            }

            if (i >= 1) { //从第二个比较
                for (int j = 0; j < charsA.length; j++) { // A字符串abc
                    if (charsB[i] == charsA[j]) {
                        right = j;
                    }
                    if (charsB[i - 1] == charsA[j]) {
                        left = j;
                    }
                }
                if (right <= left) { // 如果不是升序就+1
                    result++;
                }
            }
        }

        return result;
    }

}
